<template>
  <div>
    <div>
      <el-input v-model="jl.name" placeholder="请输入职称名称" suffix-icon="el-icon-plus" class="addJoblevelInput"/>
      <el-select size="small" v-model="jl.titleLevel" placeholder="请选择职称等级" class="selectInput">
        <el-option
            v-for="item in titleLevels"
            :key="item"
            :label="item"
            :value="item">
        </el-option>
      </el-select>
      <el-button type="primary" icon="el-icon-plus" @click="addJoblevel" class="btnAddJobLevel">添加职称</el-button>
    </div>

    <div class="jobLevelMain">
      <el-table
          :data="jls"
          border
          stripe
          style="width: 70%"
          @selection-change="handleSelectionChange">
        <el-table-column
            type="selection"
            width="55">
        </el-table-column>
        <el-table-column
            prop="id"
            label="编号"
            width="180">
        </el-table-column>
        <el-table-column
            prop="name"
            label="职称名称"
            width="180">
        </el-table-column>
        <el-table-column
            prop="titleLevel"
            label="职称等级">
        </el-table-column>
        <el-table-column
            prop="createDate"
            label="创建时间">
        </el-table-column>

        <el-table-column
            prop="enabled"
            label="是否启用">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.enabled" type="success">已启用</el-tag>
            <el-tag v-else type="danger">未启用</el-tag>
          </template>
        </el-table-column>

        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
                size="mini"
                @click="showJobLevel(scope.$index, scope.row)">编辑
            </el-button>
            <el-button
                size="mini"
                type="danger"
                @click="deleteJobLevel(scope.$index, scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-button size="small" style="margin-top: 8px" type="danger"
                 @click="deleteMany" :disabled="this.multipleSelection.length  === 0">批量删除
      </el-button>

      <el-dialog
          title="编辑职称"
          :visible.sync="dialogVisible"
          width="30%">
        <table>
          <tr>
            <td>
              <el-tag>职称名称</el-tag>
            </td>
            <td>
              <el-input v-model="updateJl.name" class="updateJlInput"/>
            </td>
          </tr>
          <tr>
            <td>
              <el-tag>职称等级</el-tag>
            </td>
            <td>
              <el-select size="small" v-model="updateJl.titleLevel" placeholder="职称等级" class="selectInput">
                <el-option
                    v-for="item in titleLevels"
                    :key="item"
                    :label="item"
                    :value="item">
                </el-option>
              </el-select>
            </td>
          </tr>
          <tr>
            <td>
              <el-tag>是否启用</el-tag>
            </td>
            <td>
              <el-switch
                  style="margin-left: 6px"
                  v-model="updateJl.enabled"
                  active-color="#13ce66"
                  inactive-color="#ff4949"
                  active-text="已启用"
                  inactive-text="未启用">
              </el-switch>
            </td>
          </tr>

        </table>

        <span slot="footer" class="dialog-footer">
                <el-button size="small" @click="dialogVisible = false">取 消</el-button>
                <el-button size="small" type="primary" @click="doUpdateJobLevel">确 定</el-button>
          </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
export default {
  name: "JoblevelMana",
  data() {
    return {
      // 输入框绑定数据
      jl: {
        name: '',
        titleLevel: ''
      },
      updateJl: {
        name: '',
        titleLevel: '',
        enabled: false
      },
      // 下拉框绑定的数据
      titleLevels: [
        '正高级',
        '副高级',
        '中级',
        '初级',
        '员级'
      ],
      // 表格绑定数据
      jls: [],
      // 多选框绑定的数据
      multipleSelection: [],
      // 控制编辑填出框
      dialogVisible: false,
    }
  },
  mounted() {
    this.initJoblevels();
  },
  methods: {
    // 初始化职称数据
    initJoblevels() {
      this.getRequest('/system/basic/jobLevel/').then(resp => {
        if (resp) {
          this.jls = resp;
          this.jl.name = '';
          this.jl.titleLevel = '';
        }
      })
    },
    // 添加职称
    addJoblevel() {
      this.postRequest('/system/basic/jobLevel/', this.jl).then(resp => {
        if (resp) {
          this.initJoblevels();
        }
      })
    },
    // 选中删除当前职称
    deleteJobLevel(index, data) {
      this.$confirm('此操作将永久删除[' + data.name + ']职称信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteRequest('/system/basic/jobLevel/' + data.id).then(resp => {
          if (resp) {
            this.initJoblevels();
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    // 当前职称
    editJobLevel(index, data) {
      this.putRequest('/system/basic/jobLevel/', data).then(resp => {
        if (resp) {
          this.initJoblevels();
        }
      })
    },
    // 展示当前选择的职位信息
    showJobLevel(index, data) {
      Object.assign(this.updateJl, data);
      this.dialogVisible = true;
    },
    // 更新当前职位
    doUpdateJobLevel() {
      this.putRequest('/system/basic/jobLevel/', this.updateJl).then(resp => {
        if (resp) {
          this.initJoblevels();
          this.dialogVisible = false;
        }
      })
    },
    // 选择框发送变化时
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 批量删除
    deleteMany() {
      this.$confirm('此操作将永久删除[' + this.multipleSelection.length + ']条职称信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let ids = '?';
        this.multipleSelection.forEach(item => {
          ids += 'ids=' + item.id + '&';
        })
        this.deleteRequest('/system/basic/jobLevel/' + ids).then(resp => {
          if (resp) {
            this.initJoblevels();
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      })
    },
  }
}
</script>

<style scoped>
.addJoblevelInput {
  width: 300px;
  margin-right: 10px;
}


.jobLevelMain {
  margin-top: 10px;
}

.btnAddJobLevel {
  margin-left: 10px;
}
</style>
